using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Sites : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnAddNew.Visible = true;
        btnAddNew.Text = "Add New";
        btnAddNew.Click += new EventHandler(btnAddNew_Click);

        dsSites.GetData += new EventHandler<Acms.DataControls.BaseDataSourceViewGetDataEventArgs<Acms.Bll.SiteCollection,Acms.Bll.Site>>(dsSites_GetData);

        gvSites.RowCommand += new GridViewCommandEventHandler(gvSites_RowCommand);
        gvSites.SelectedIndexChanged += new EventHandler(gvSites_SelectedIndexChanged);
        
        if (!IsPostBack)
        {
            if (base.SiteId.HasValue)
            {
                int i = -1;
                foreach (Acms.Bll.Site s in SitesData)
                {
                    i++;
                    if (s.Id == base.SiteId.Value)
                    {
                        gvSites.SelectedIndex = i;
                        break;
                    }
                }
            }
        }
    }

    void gvSites_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            base.SiteId = (int)gvSites.SelectedValue;
        }
        catch (InvalidCastException)
        {
            base.SiteId = null;  
        }
    }

    void gvSites_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Selected")
        {
            Trace.Warn("Selected", e.CommandArgument.ToString());
        }
    }

    private Acms.Bll.SiteCollection _sites;
    protected Acms.Bll.SiteCollection SitesData
    {
        get
        {
            if (_sites == null)
            {
                _sites = Acms.Bll.SiteCollection.GetAll();
            }
            return _sites;
        }
    }

    void dsSites_GetData(object sender, Acms.DataControls.BaseDataSourceViewGetDataEventArgs<Acms.Bll.SiteCollection, Acms.Bll.Site> e)
    {
        e.SetData(SitesData);
    }

    void btnAddNew_Click(object sender, EventArgs e)
    {
        gvSites.StartInsert();
    }
}
